package com.person.ntv.service.sys.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.person.common.InitApplication;
import com.person.exception.ServiceException;
import com.person.ntv.dao.sys.CommonMapper;
import com.person.ntv.entity.sys.SysCode;
import com.person.ntv.service.sys.CommonService;
@Service("commonService")
public class CommonServiceImpl implements CommonService{
	@Autowired
	private CommonMapper commonMapper;
	
	@Override
	public int validateOne(String tableName, String colName, String colValue,
			String id) throws ServiceException {
		try{
			 Map<String,String>params=new HashMap<String,String>();
			 params.put("tableName",tableName);
			 params.put("colName",colName);
			 params.put("colValue",colValue);
			 params.put("id",id);
			return commonMapper.validateOne(params);
			}catch (Exception e) {
		          e.printStackTrace();
		          throw new ServiceException("系统错误。");
		   }
	}
	
	@Override
	public List<SysCode> getCodeList(String csEnName,boolean defaultOpt) throws ServiceException {
		try{
			//List<SysCode>list = commonMapper.getCodeList(csEnName);
			List<SysCode>list=InitApplication.codeMap.get(csEnName);
			List<SysCode>list2=new ArrayList<SysCode>();
			if(list!=null&&defaultOpt){
				SysCode sc=new SysCode();
				sc.setItemValue("");
				sc.setItemName("请选择");//sc.setItemName("请选择");
				list2.add(0, sc);
			}
			list2.addAll(list);
			return list2;
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("系统错误");
		}
	}
	@Override
	public String getCodeName(String csEnName, String codeValue)
			throws ServiceException {
		try{
			//return commonMapper.getCodeName(csEnName, codeValue);
			List<SysCode>list=InitApplication.codeMap.get(csEnName);
			if(list!=null){
				for(SysCode sc:list){
					if(sc.getItemValue()!=null&&sc.getItemValue().equals(codeValue))
						return sc.getItemName();
				}
			}
			return null;
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("系统错误");
		}
	}
	
	@Override
	public String getCodeName(List<SysCode> codeList, String codeValue)
			throws ServiceException {
		if(codeList!=null&&codeValue!=null){
			for(SysCode sc:codeList){
				if(codeValue.equals(sc.getItemValue()))
					return sc.getItemName();
			}
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getCodeSetList() throws ServiceException {
		try{
			return commonMapper.getCodeSetList();
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("系统错误");
		}
	}
	
}
